﻿using System;
using System.IO;
using System.Reflection;

namespace UFSoft.UBF.Cust.ClassView.MVC.Resource
{
    /// <summary>
    ///     资源帮助类
    /// </summary>
    public static class ResourceHelper
    {
        /// <summary>
        ///     获取模板内容
        /// </summary>
        /// <returns></returns>
        public static string GetFileContent(string fileName)
        {
            //获得正在运行类所在的名称空间
            Type type = MethodBase.GetCurrentMethod().DeclaringType;
            if (type == null) return string.Empty;
            string _namespace = type.Namespace;
            //获得当前运行的Assembly
            Assembly _assembly = Assembly.GetExecutingAssembly();
            //根据名称空间和文件名生成资源名称
            string resourceName = string.Format("{0}.{1}", _namespace, fileName);
            //根据资源名称从Assembly中获取此资源的Stream
            Stream stream = _assembly.GetManifestResourceStream(resourceName);
            if (stream == null || stream.Length == 0) return string.Empty;
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            return reader.ReadToEnd();
        }
    }
}